<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['messageId']"/>
      </a-form-item>
      <a-form-item style="display:none">
        <a-input v-decorator="['startDate']"/>
      </a-form-item>
      <a-form-item style="display:none">
        <a-input v-decorator="['endDate']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发布周期">
        <a-range-picker @change="onChange" v-decorator="['time',{rules: [{required: true}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题">
        <a-input placeholder="" v-decorator="['messageHead',{rules: [{required: true, message: ''}]}]"/>
      </a-form-item>
      <a-form-item
        label="添加描述"
        :labelCol="{lg: {span: 5}, sm: {span: 7}}"
        :wrapperCol="{lg: {span: 16}, sm: {span: 17} }">
        <a-textarea
          rows="4"
          placeholder="请输入你阶段性工作目标"
          v-decorator="[
            'messageBody',
            {rules: [{ required: true, message: '请输入目标描述' }]}
          ]" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveMessage } from '@/api/shifts/message'
import pick from 'lodash.pick'
export default {
  name: 'DriverMsgModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 5 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 10 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
  },
  methods: {
    // 日期控件的初始值
    onChange (date, dateString) {
      // console.log(date)
      // this.form.setFieldsValue({ testEndDate: testEndDate })
      this.form.setFieldsValue({ startDate: dateString[0] })
      this.form.setFieldsValue({ endDate: dateString[1] })
      console.log(dateString)
    },
    add () {
      this.form.resetFields()
      this.edit({ messageId: 0 })
    },
    edit (record) {
      this.mdl = Object.assign(record)
      this.visible = true
      var startDate
      var endDate
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'messageId', 'messageHead', 'messageBody', 'startDate', 'endDate'))
        startDate = this.$moment(record.startDate).format('YYYY-MM-DD')
        endDate = this.$moment(record.endDate).format('YYYY-MM-DD')
        this.form.setFieldsValue({ time: [this.$moment(startDate, 'YYYY-MM-DD  HH:mm:ss'), this.$moment(endDate, 'YYYY-MM-DD  HH:mm:ss')] })
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveMessage(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
